package louie.wendy.redisrepository.controller;

import louie.wendy.redisrepository.entity.User;
import louie.wendy.redisrepository.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author louie
 */

@RestController
@RequestMapping(value = "/user")
public class UserController {
    private final UserService userService;

    public UserController (UserService userService) {
        this.userService = userService;
    }

    @PostMapping
    public ResponseEntity<String> createUser (@RequestBody User user) {
        userService.save(user);
        return new ResponseEntity<>("user created ...", HttpStatus.OK);
    }

    @GetMapping(value = "/{userId}")
    public User getUser (@PathVariable String userId) {
        return userService.findUser(userId);
    }

    @PutMapping
    public ResponseEntity<String> updateUser (@RequestBody User user) {
        userService.update(user);
        return new ResponseEntity<>("user updated ...", HttpStatus.OK);
    }

    @DeleteMapping(value = "/{userId}")
    public ResponseEntity<String> deleteUser (@PathVariable String userId) {
        userService.delete(userId);
        return new ResponseEntity<>("user deleted ...", HttpStatus.OK);
    }
}
